require( '$d/css/common.less' );
require( './style.less' );
define( [
  'jquery',
  'require',
  'header',
  'clipboard',
  "message",
  "api"
], function( $, require ) {
  var cb = require( "clipboard" );
  var ShowTitleMessage = require( "message" );
  var showTime = ShowTitleMessage.showTime;
  var thisApi = require( "api" );

  var public_vars = public_vars || {};;
  ( function( $, window, undefined ) {
    'use strict';
    $( function() {
      public_vars.$container = $( '.content' );
      public_vars.$selct = public_vars.$container.find( '.tag_left_text' );
      public_vars.$clean = public_vars.$container.find( '.clean_text' );
      public_vars.$extract = public_vars.$container.find( '.extract_tag' );
      public_vars.$tag = public_vars.$container.find( '.tag_list' );
      public_vars.$copy = public_vars.$container.find( '.copy_label' );

      public_vars.$clean.click( monitor );
      public_vars.$extract.click( etactag );
      // public_vars.$copy.click(copy);
      var clipboard = new cb( '.copy_label' );
      clipboard.on( 'success', function( e ) {
        ShowTitleMessage.show( '复制成功！', 1000 );
      } );
      clipboard.on( 'error', function( e ) {
        ShowTitleMessage.showWarning( '您使用的浏览器暂不支持此功能，请手动复制文本框中内容！', 3500 );
      } );
    } )
  } )( $, window );

  function monitor() {
    public_vars.$selct.val( '' );
    $( '.tag_list' ).empty();

    if ( public_vars.$selct.val().length ) {
      public_vars.$copy.addClass( 'btn-success' ).attr( "disabled", false );
    } else {
      public_vars.$copy.removeClass( 'btn-success' ).attr( "disabled", true );
    }
  }

  function etactag( argument ) {
    var val = public_vars.$selct.val();
    if ( !val ) {
      ShowTitleMessage.showWarning( '请输入内容', showTime );
      return;
    }
    ShowTitleMessage.show( '正在提取中...' );
    $.ajax( {
      type: 'post',
      url: thisApi.SMART_TAG.RECOMMEND_TAG,
      data: {
        content: val
      },
      success: function( data ) {
        var data = eval( '(' + data + ')' );
        var html = '';
        var val = '';
        if ( data.data && data.data.length != 0 ) {
          for ( i in data.data ) {
            var tags = data.data[ i ];
            html += '<span>' + tags + '</span>';
            if ( val.length > 1 ) {
              val += ','
            }
            val += tags;
          }
          public_vars.$copy.addClass( 'btn-success' ).attr( "disabled", false );
          ShowTitleMessage.hide();
        } else {
          public_vars.$copy.removeClass( 'btn-success' ).attr( "disabled", true );
          ShowTitleMessage.showWarning( '文本中没有可以提取的标签', showTime );
        }
        $( '.tag_list' ).empty().append( html );
        // $('#tag_list').val(val);
        public_vars.$copy.attr( 'data-clipboard-text', val );
      }
    } )
  }
} );
